package com.yztctech.a25_03_bindservice.base;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;

public class MyBaseService extends Service {
    private String TAG = "MyBaseService";

    /**
     * @desc 生命周期.创建触发1次
     */
    @Override
    public void onCreate() {
        super.onCreate();
        // 通用初始化代码
        Log.e("Tag", TAG + "------------> onCreate");

    }

    /**
     * @param intent
     * @return
     * @desc 绑定成功后(即bindService后)触发1次;为了拿到service实例
     */
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.e("Tag", TAG + "------------> onBind");
        return new MyBaseBinder();//返回一个自定义的binder
    }

    protected class MyBaseBinder extends Binder {

        //获得Service对象
        public MyBaseService getService() {
            return MyBaseService.this;
        }

    }

    /**
     * @desc 生命周期.对于bindService，它不会触发此方法
     */
    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        Log.e("Tag", TAG + "------------> onStart");
    }

    /**
     * @desc 生命周期(对于bindService ， 它不会触发此方法)
     */
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // 处理启动命令的逻辑
        Log.e("Tag", TAG + "------------> onStartCommand");
        return super.onStartCommand(intent, flags, startId);
    }


    /**
     * @notice 注意这里如果耗时操作要开启线程
     * @desc 处理业务方法
     */
    protected void loadPicture(String url) {
        Log.e("Tag", TAG + "------------> loadPicture url == " + url);
    }

    protected void loadMedia(String url) {
        Log.e("Tag", TAG + "------------> loadMedia url == " + url);
    }


    /**
     * @param intent
     * @return
     * @desc 解除绑定触发
     */
    @Override
    public boolean onUnbind(Intent intent) {
        Log.e("Tag", TAG + "------------> onUnbind");
        return super.onUnbind(intent);
    }

    /**
     * @desc 生命周期.销毁时触发1次
     */
    @Override
    public void onDestroy() {
        super.onDestroy();
        // 清理资源
        Log.e("Tag", TAG + "------------> onDestroy");
    }
}
